<?php

namespace falcon5\inc\Http\Controllers;

use falcon5\inc\Base\WPOption;
use falcon5\inc\Http\JsonResponse;
use falcon5\inc\Http\Request;
use falcon5\inc\Infrastructure\Services\CacheService;
use falcon5\inc\Infrastructure\Services\SiteService;
use hollisho\helpers\ArrayHelper;
use hollisho\helpers\SignatureValidatorHelper;

/**
 * Class CallbackController
 * @package falcon5\inc\Http\Controllers
 */
class CallbackController extends BaseController
{

    private function parseData($data)
    {
        /** @var WPOption $falcon5Option */
        $falcon5Option = falcon5_app(WPOption::class);
        $data = json_decode($data, true);

        if (empty($data)) {
            throw new \Exception('parse data false');
        }

        $verifySignature = SignatureValidatorHelper::verifySignature($data, $falcon5Option->getApiKey());

        if (!$verifySignature) {
            throw new \Exception('verify signature false');
        }

        return $data;
    }

    /**
     * 站点信息回调
     * @param Request $request
     * @return JsonResponse
     */
    public function siteInfoCallback(Request $request): JsonResponse
    {
        try {
            $data = $request->get('data');
            $data = $this->parseData($data);
            $action = ArrayHelper::getValue($data, 'action');

            if ($action) {
                /** @var SiteService $siteService */
                $siteService = falcon5_app(SiteService::class);
                switch ($action) {
                    case 'update':
                        $siteService->updateSite($data);
                        break;
                    case 'delete':
                        $siteService->delSite($data);
                        break;
                }
            }

            return JsonResponse::success($data);
        } catch (\Exception $e) {
            falcon5_error_log($e->getMessage());
            return JsonResponse::error($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 站点信息回调
     * @param Request $request
     * @return JsonResponse
     */
    public function entryInfoCallback(Request $request): JsonResponse
    {
        try {
            $data = $request->get('data');
            $data = $this->parseData($data);
            $action = ArrayHelper::getValue($data, 'action');

            if ($action) {
                /** @var CacheService $cacheService */
                $cacheService = falcon5_app(CacheService::class);
                switch ($action) {
                    case 'save':
                        $pageId = ArrayHelper::getValue($data, 'page_id');
                        $lang = ArrayHelper::getValue($data, 'translated_lang');
                        $pageId && $lang && $cacheService->delCacheByPageId($pageId, $lang);
                        break;
                }
            }

            return JsonResponse::success($data);
        } catch (\Exception $e) {
            falcon5_error_log($e->getMessage());
            return JsonResponse::error($e->getCode(), $e->getMessage());
        }
    }
}